using System;
using System.Collections.Generic;
using System.Linq;

namespace demo
{
    /// <summary>
    /// 门诊就诊信息类
    /// 用于解析形如"outpVisitId@11704441_patientId@1410713856_patiName@陆琳艾"的消息内容
    /// </summary>
    public class OutpatientVisitInfo
    {
        /// <summary>
        /// 门诊就诊ID
        /// </summary>
        public string OutpVisitId { get; set; }

        /// <summary>
        /// 患者ID
        /// </summary>
        public string PatientId { get; set; }

        /// <summary>
        /// 患者姓名
        /// </summary>
        public string PatiName { get; set; }

        /// <summary>
        /// 门诊科室名称
        /// </summary>
        public string OutpSpecialName { get; set; }

        /// <summary>
        /// 就诊日期
        /// </summary>
        public string OutpDate { get; set; }

        /// <summary>
        /// 就诊ID
        /// </summary>
        public string VisitId { get; set; }

        /// <summary>
        /// 接诊医生ID
        /// </summary>
        public string AdmissionDoctor { get; set; }

        /// <summary>
        /// 二维码信息
        /// </summary>
        public string QrCode { get; set; }

        /// <summary>
        /// 挂号费状态
        /// </summary>
        public string RegistrationFeeStatus { get; set; }

        /// <summary>
        /// 原始消息内容
        /// </summary>
        public string RawContent { get; set; }

        /// <summary>
        /// 其他未识别的键值对
        /// </summary>
        public Dictionary<string, string> OtherProperties { get; set; }

        public OutpatientVisitInfo()
        {
            OtherProperties = new Dictionary<string, string>();
        }

        /// <summary>
        /// 从特定格式的字符串解析门诊就诊信息
        /// 格式："key1@value1_key2@value2_key3@value3"
        /// </summary>
        /// <param name="content">要解析的内容</param>
        /// <returns>解析后的门诊就诊信息对象</returns>
        public static OutpatientVisitInfo Parse(string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                return null;
            }

            var result = new OutpatientVisitInfo
            {
                RawContent = content
            };

            // 按_分割不同的键值对
            string[] pairs = content.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
            
            foreach (string pair in pairs)
            {
                // 按@分割键和值
                string[] keyValue = pair.Split(new[] { '@' }, 2, StringSplitOptions.None);
                if (keyValue.Length == 2)
                {
                    string key = keyValue[0];
                    string value = keyValue[1];

                    // 根据键名设置相应的属性
                    switch (key.ToLower())
                    {
                        case "outpvisitid":
                            result.OutpVisitId = value;
                            break;
                        case "patientid":
                            result.PatientId = value;
                            break;
                        case "patiname":
                            result.PatiName = value;
                            break;
                        case "outpspecialname":
                            result.OutpSpecialName = value;
                            break;
                        case "outpdate":
                            result.OutpDate = value;
                            break;
                        case "visitid":
                            result.VisitId = value;
                            break;
                        case "admissiondoctor":
                            result.AdmissionDoctor = value;
                            break;
                        case "qrcode":
                            result.QrCode = value;
                            break;
                        case "registrationfeestatus":
                            result.RegistrationFeeStatus = value;
                            break;
                        default:
                            // 存储未识别的键值对
                            result.OtherProperties[key] = value;
                            break;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 将对象转换为字符串表示
        /// </summary>
        /// <returns>对象的字符串表示</returns>
        public override string ToString()
        {
            return $"患者: {PatiName}, 科室: {OutpSpecialName}, 日期: {OutpDate}, 挂号费状态: {RegistrationFeeStatus}";
        }
    }
}